home *** CD-ROM | disk | FTP | other *** search
- Path: gramercy.ios.com!lalit
- From: lalit@gramercy.ios.com (lalit gidwani)
- Newsgroups: comp.lang.c
- Subject: Re: Unix Domain Socket problem...
- Date: 4 Jan 1996 02:17:09 GMT
- Organization: Internet Online Services
- Distribution: Zollen
- Message-ID: <4cfdb5$keo@news2.ios.com>
- References: <DKEstL.JBB@ariel.cs.yorku.ca>
- NNTP-Posting-Host: gramercy.ios.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- STEPHEN HOI CHI KONG (cs911319@ariel.cs.yorku.ca) wrote:
- : /*
- : * Unix Domain Socket
- : *
- : * Child process passes a file descriptor to its parent by using
- : * Unix domain socket.
- : *
- : * Function 'my_open' fork a child process. The child process
- : * then open a file and return a file descriptor and send the
- : * fd back to parent using 'sendmsg', parent receive the fd by
- : * using 'recvmsg'.
- : *
- : * I am learing this socket but for no reason, it core dumped on
- : * my solaris II system.
- : *
- : * Could any expert out there pinpoint any bugs so that I can
- : * relieve my headache please....
- : */
-
- : #include <sys/types.h>
- : #include <sys/socket.h>
- : #include <sys/uio.h>
-
-
- : #define BUFFER_SIZE 4096
-
-
- : int my_open (char *name, int mode);
- : int s_pipe (int sfd[]);
- : int send_sock (int sockfd, int fd);
- : int recv_sock (int sockfd);
-
-
-
- : main(int argc, char **argv)
- : {
- : int n, fd;
- : char buff[BUFFER_SIZE];
- :
- : if (argc != 2) {
- : printf ("Usage: %s [filename]\n", argv[0]);
- : exit(1);
- : }
- :
- : if ((fd = my_open (argv[1], 0)) < 0) {
- : printf ("Error opening file.\n");
- : exit (1);
- : }
- :
- : while ((n = read (fd, buff, BUFFER_SIZE)) > 0)
- : if (write (1, buff, n) != n)
- : printf ("Write error!\n");
- : }
-
-
- : int my_open (char *name, int mode)
- : {
- : int desc[2], fd;
- :
- : if (s_pipe (desc) < 0)
- : return -1;
- :
- : if (fork() != 0) { /* parent process */
- :
- : close (desc[1]);
- :
- : fd = recv_sock (desc[0]);
- :
- : wait();
- : close (desc[0]);
- :
- : return fd;
- : }
- : else { /* must be child */
- :
- : close (desc[0]);
- :
- : fd = open (name, mode);
- : send_sock (desc[1], fd);
- :
- : close (desc[1]);
- :
- : exit (0);
- : }
- : }
- :
- : int s_pipe (int sfd[])
- : {
- : return socketpair (AF_UNIX, SOCK_STREAM, 0, sfd);
- : }
-
- : int send_sock (int sockfd, int fd)
- : {
- : struct iovec iov[1];
- : struct msghdr msg;
- : extern int errno;
- :
- :
- : iov[0].iov_base = (char *) 0; /* no data to send */
- : iov[0].iov_len = 0;
- :
- : msg.msg_iov = iov;
- : msg.msg_iovlen = 1;
- : msg.msg_name = (caddr_t) 0;
- : msg.msg_accrights = (caddr_t) &fd;
- : msg.msg_accrightslen = sizeof(fd);
- :
- : if (sendmsg (sockfd, &msg, 0) < 0)
- : return ((errno > 0) ? errno : 255);
- :
- : return 0;
- : }
-
- : int recv_sock (int sockfd)
- : {
- : int fd;
- : struct iovec iov[1];
- : struct msghdr msg;
- :
- : iov[0].iov_base = (char *) 0;
- : iov[0].iov_len = 0;
- :
- : msg.msg_iov = iov;
- : msg.msg_iovlen = 1;
- : msg.msg_name = (caddr_t) 0;
- : msg.msg_accrights = (caddr_t) &fd;
- : msg.msg_accrightslen = sizeof (fd);
- :
- : if (recvmsg (sockfd, &msg, 0) < 0)
- : return -1;
- :
- : return fd;
- : }
-